<template>
  <el-dialog
    v-model="dialogFormVisible"
    title="添加题目"
    width="900"
    top="5vh"
    style="height: 830px"
  >
    <el-select
      v-model="questionType"
      placeholder="单选题"
      size="large"
      style="width: 240px; margin-bottom: 10px"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
        @click="changeRoute(item.route)"
      />
    </el-select>
    <el-scrollbar height="650px">
      题干：
      <WangEditor ref="stem"></WangEditor>
      <RouterView />
    </el-scrollbar>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleContent()"> 添加 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, defineExpose, defineEmits } from 'vue'
import { useRouter } from 'vue-router'
import WangEditor from '@/components/WangEditor.vue'

const emit = defineEmits(['submit'])
const dialogFormVisible = ref(false)
const questionType = ref('单选题')
const QtypeRoute = useRouter()
const stem = ref('stem')
let newQuestions = ref(JSON.parse(localStorage.getItem('newQuestions')) || [])
const options = [
  {
    route: 'ChoiceQues',
    value: '单选题',
    label: '单选题',
  },
  {
    route: 'ChoiceQues',
    value: '多选题',
    label: '多选题',
  },
  {
    route: 'ScoreQues',
    value: '打分题',
    label: '打分题',
  },
  {
    route: 'FillQues',
    value: '填空题',
    label: '填空题',
  },
  {
    route: 'SortQues',
    value: '排序题',
    label: '排序题',
  },
  {
    route: 'ShortAnswer',
    value: '简答题',
    label: '简答题',
  },
]

defineExpose({
  dialogFormVisible,
})

function changeRoute(route) {
  if (route === 'ShortAnswer') {
    QtypeRoute.push(`/CreateQN`)
    return
  }
  QtypeRoute.push(`/CreateQN/${route}`)
}
function handleContent() {
  dialogFormVisible.value = false
  const baseURL = QtypeRoute.currentRoute.value.matched[1].instances.default
  const optionContents = []
  if (baseURL.optionContent) {
    for (let i = 0; i < baseURL.optionContent.length; i++) {
      optionContents.push(baseURL.optionContent[i].valueHtml)
    }
  }
  newQuestions.value.push({
    题干: stem.value.valueHtml,
    题型: questionType.value,
    id: Date.now(),
    ...(baseURL.optionContent && { 选项: optionContents }),
    ...(baseURL.scoreItems && { 打分项: baseURL.scoreItems }),
    ...(baseURL.scoreCount && { 打分等级: baseURL.scoreCount }),
    ...(baseURL.fillCount && { 填空数量: baseURL.fillCount }),
    ...(baseURL.list && { 顺序: baseURL.list }),
  })
  localStorage.setItem('newQuestions', JSON.stringify(newQuestions.value))
  const localQuestions = JSON.parse(localStorage.getItem('newQuestions') || [])
  emit('submit', localQuestions)
  if (baseURL.optionContent) {
    for (let i = 0; i < baseURL.optionContent.length; i++) {
      baseURL.optionContent[i].valueHtml = '<p></p>'
    }
  } //清空选项
  if (baseURL.list) {
    for (let i = 0; i < baseURL.list.length; i++) {
      baseURL.list[i].name = '1'
    }
  } //清空排序项
  stem.value.valueHtml = '<p></p>'
}
</script>

<style>
</style>